<?php
class MemberAction extends CommonAction {
    public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('Member');
    }
   
    public function index(){
		$thisPage = $this->pageInfo['p'] = I('p', 1);
		
		if($this->pageInfo['username'] = I('username', '')){
			$map['username'] = array('LIKE', '%'.$this->pageInfo['username'].'%');
		}
		
		if($this->pageInfo['status'] = I('status', '')){
			$map['status'] = $this->pageInfo['status'];
		}
		
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage);
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage);
		$this->assign('page',$page);
		
		session('pageInfo', $this->pageInfo);
		$this->assign('pageInfo', $this->pageInfo);
		$this->assign('list', $list);
		$this->display();
    }
	
	public function edit(){
		$id = I('id', 0, 'intval');
		$info = $this->modelObject->getInfo($id);
		if(IS_POST){
			$username    = I('username', '', 'trim');
			$password    = I('password', '', 'trim');
			$relpassword = I('relpassword', '', 'trim');
			if(!(empty($password) && empty($relpassword))){
				if($password != $relpassword){
					$this->error('两次密码输入错误!');
				}else{
					$_POST['password'] = setPassword($password, $info['pwd_salt']);	
				}
			}else{
				unset($_POST['password']);	
			}
			
			if($this->modelObject->create()){
				if($this->modelObject->save()){
					$this->success('编辑成功', U('index', session('pageInfo')));	
				}else{
					$this->error('编辑失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			if($id){
				$this->assign('info', $info);
				$this->display();	
			}else{
				$this->error('参数错误!');
			}
		}
	}
	
	public function del(){
		$id = I('id', 0, 'intval');
		if($id){
			if($this->modelObject->deleteInfo($id)){
				$this->success('删除成功', U('index', session('pageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function delAll(){
		$id = I('id', 0, 'intval');
		$idStr = implode(',', $id);
		if(!empty($idStr)){
			if($this->modelObject->deleteInfo($idStr)){
				$this->success('删除成功', U('index', session('pageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
}